﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VietPublisher.BSL.BK;
using VietPublisher.Repository.Repositories.Concrete;



namespace VietPublisher.Repositories.Interface
{
    public class AuthorRepository : BaseRepository, IAuthorRepository
    {

        public AuthorRepository()
        {

        }


        public T_BK_AuthorGroup GetAuthorGrpByBookID(int bookID)
        {
            //var authorGroup = conte
            var authorGroup = (from ag in ContextBookStore.T_BK_AuthorGroup
                               join bk in ContextBookStore.T_BK_Book
                               on ag.AuthorGroupID equals bk.AuthorGroupID
                               select ag
                               )
                               .FirstOrDefault()
                               ;
            return authorGroup;

        }

        public List<T_BK_Author> GetAuthorList(int authorGroupID)
        {
            List<int> AuthorInGroup= new List<int>();
            var tmp = ContextBookStore.T_BK_AuthorGroup.FirstOrDefault(m=>m.AuthorGroupID== authorGroupID);
            if(tmp!= null){
               var auth=  tmp.AuthorIds.Split(',');
                foreach(var i in auth){
                    if(string.IsNullOrEmpty(i)){
                        continue;
                    }
                    AuthorInGroup.Add(int.Parse(i));

                }
            }
            var authors = ContextBookStore.T_BK_Author.Where(p => AuthorInGroup.Contains(p.AuthorID)).ToList();
            return authors;
        }
        public List<T_BK_Author> GetAuthorList(string AuthorName)
        {
            var authors = ContextBookStore.T_BK_Author.Where(p => p.AuthorName.Contains(AuthorName)).ToList();
            return authors;
        }

        public int UpdateAuthor(T_BK_Author author)
        {
            ContextBookStore.T_BK_Author.ApplyCurrentValues(author);
            int effectedRows = ContextBookStore.SaveChanges();
            return effectedRows;
        }

        public int AddAuthor(T_BK_Author author)
        {
            ContextBookStore.T_BK_Author.AddObject(author);
            int effectedRows = ContextBookStore.SaveChanges();
            return effectedRows;
        }

        public int AddAuthorGrp(T_BK_AuthorGroup authorGroup)
        {
            //add a authorGroup
            ContextBookStore.T_BK_AuthorGroup.AddObject(authorGroup);
             ContextBookStore.SaveChanges();
             int effectedRows = authorGroup.AuthorGroupID;
            return effectedRows;
        }

        public int AddAuthorGrp(string authorIds)
        {
            int authorGrpID = 0;

            #region add authorGroup
            T_BK_AuthorGroup authorGrp = new T_BK_AuthorGroup()
            {
                AuthorIds = authorIds
            };
            authorGrpID = AddAuthorGrp(authorGrp);

            #endregion

            //#region add author
            //foreach (var authorName in authorNames)
            //{
            //    T_BK_Author author = new T_BK_Author()
            //    {
            //        AuthorGroupID = authorGrpID,
            //        AuthorName = authorName
            //    };
            //    AddAuthor(author);//add a author
            //}
            //#endregion

            return authorGrpID;
        }

        public List<T_BK_Author> GetAuthorList()
        {
            var authors = ContextBookStore.T_BK_Author.ToList();
            return authors;
        }

        //public int GetAuthorGrp(string authorName)
        //{
        //    int authorGrpID = 0;
        //    var author = ContextBookStore
        //                .T_BK_Author.Where(a => a.AuthorName == authorName)
        //                .FirstOrDefault();
        //    if (author != null)
        //    {
        //        authorGrpID = author.AuthorGroupID;
        //    }
        //    return authorGrpID;

        //}

        public bool HasAuthorGrp(List<string> authorNames)
        {
            bool hasAuthorGroup = true;

            foreach (var authorName in authorNames)
            {
                var author = ContextBookStore
                            .T_BK_Author
                            .Where(p => p.AuthorName.Contains(authorName))
                            .FirstOrDefault();
                if (author == null)
                {
                    hasAuthorGroup = false;
                    break;
                }
            }

            return hasAuthorGroup;
        }
    }
}
